import { AxiosRequestConfig, AxiosStatic } from './types/index';
import { extend } from './helpers/utils';
import Axios from './core/Axios';
import defaults from './defaults';
import mergeConfig from './core/mergeConfig';
import Cancel, { isCancel } from './cancel/Cancel';
import CancelToken from './cancel/CancelToken';

function createAxiosInstance(config: AxiosRequestConfig): AxiosStatic {
  const context = new Axios(config);
  const instance = Axios.prototype.request.bind(context);
  extend(instance, context);
  return instance as AxiosStatic;
}

const axios = createAxiosInstance(defaults);

axios.create = function create(config?: AxiosRequestConfig) {
  return createAxiosInstance(mergeConfig(defaults, config));
};
axios.Cancel = Cancel;
axios.CancelToken = CancelToken;
axios.isCancel = isCancel;
export default axios;
